package com.jingjiu.homeservicesapi.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.jingjiu.homeservicesapi.enums.ResultEnum;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
public class R<T> implements Serializable {

	@Serial
	private static final long serialVersionUID = 1L;


	private int code;


	private String msg;


	private T data;

	@JsonProperty("success")
	private boolean isSuccess;

	public static <T> R<T> ok() {
		return restResult(null, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);

	}

	public static <T> R<T> ok(T data) {
		return restResult(data, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
	}

	public static <T> R<T> restResult(T data, ResultEnum resultEnum) {
		R<T> apiResult = new R<>();
		apiResult.setCode(resultEnum.getCode());
		apiResult.setData(data);
		apiResult.setMsg(resultEnum.getMsg());
		return apiResult;
	}

	public static <T> R<T> failed(ResultEnum resultEnum) {
		return restResult(null, resultEnum.getCode(), resultEnum.getMsg(), true);
	}


	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, ResultEnum.SUCCESS.getCode(), msg, true);
	}

	public static <T> R<T> failed() {
		return restResult(null, ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), false);
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, ResultEnum.FAIL.getCode(), msg, false);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), false);
	}

	public static <T> R<T> failed(T data, String msg) {
		return restResult(data, ResultEnum.FAIL.getCode(), msg, false);
	}
	public static <T> R<T> failed(int code, String msg) {
		return restResult(null, code, msg, false);
	}

	public static <T> R<T> restResult(T data, int code, String msg, boolean isSuccess) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		apiResult.setSuccess(isSuccess);
		return apiResult;
	}

	public static <T> R<T> restResult(T data, int code, String msg) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);


		apiResult.setSuccess(code == ResultEnum.SUCCESS.getCode());
		return apiResult;
	}

}
